package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.commom.R;
import com.itheima.ydd.commom.UserIdContext;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddUserData;
import com.itheima.ydd.exception.CustomException;
import com.itheima.ydd.mapper.YddArticleMapper;
import com.itheima.ydd.service.YddArticleService;
import com.itheima.ydd.service.YddUserDataService;
import com.itheima.ydd.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {
    @Autowired
    private YddArticleMapper yddArticleMapper;

    @Autowired
    private YddUserDataService yddUserDataService;

    /**
     * 分页查询
     *
     * @param startTime
     * @param endTime
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    public R list(Long startTime, Long endTime, String title, Integer cateid, int page, int limit) {
        Page<YddArticle> articlePage = new Page<>(page, limit);

        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        //模糊查标题title，相同eq查id要一样cateid，在开始和结束之间用between
        wrapper.like(StringUtils.isNotEmpty(title), YddArticle::getTitle, title)
                .eq(cateid != null, YddArticle::getCateid, cateid)
                .between(startTime != null && endTime != null, YddArticle::getCreateDate, startTime, endTime);

        yddArticleMapper.selectPage(articlePage, wrapper);
        //调用工具类
        PageUtils<YddArticle> bean = new PageUtils<>(articlePage);

        return R.success("", bean);
    }

    @Override
    @Transactional
    public void addData(Integer type, Integer article_id) {
        //获取当前登录用户
        String userId = UserIdContext.getCurrentId();

        //根据用户id和文章id查询当前文章是否已经被收藏或者点赞
        LambdaQueryWrapper<YddUserData> userDataWrapper = new LambdaQueryWrapper<>();
        userDataWrapper.eq(article_id != null, YddUserData::getArticleId, article_id);
        userDataWrapper.eq(YddUserData::getUserId, userId);
        userDataWrapper.eq(YddUserData::getType,type);
        YddUserData one = yddUserDataService.getOne(userDataWrapper);
        if (one != null) {
            //说明已经被收藏了 ，抛出异常
            if (type == 1) {
                throw new CustomException("文章已收藏");
            }
            if (type == 2) {
                throw new CustomException("文章已点赞");
            }
        }

        //添加点赞或者收藏
        YddUserData yddUserData = new YddUserData();
        yddUserData.setUserId(Integer.valueOf(userId));
        yddUserData.setArticleId(article_id);
        yddUserData.setType(type);

        //保存基本信息
        yddUserDataService.save(yddUserData);
        //如果是点赞还需要增加数量
        if (type == 2) {
            YddArticle byId = this.getById(article_id);
            String lovenum = byId.getLovenum();
            if (lovenum == null || lovenum.equals("")) {
                lovenum = "0";
            }
            Integer newLoveNum = Integer.parseInt(lovenum);
            byId.setLovenum(String.valueOf(newLoveNum + 1));
            //更新数据
            this.updateById(byId);
        }
    }

    @Override
    @Transactional
    public void delData(Integer type, Integer article_id) {
        //获取当前登录用户
        String userId = UserIdContext.getCurrentId();

        //取消点赞或收藏
        LambdaQueryWrapper<YddUserData> userDataWrapper = new LambdaQueryWrapper<>();
        userDataWrapper.eq(article_id != null, YddUserData::getArticleId, article_id);
        userDataWrapper.eq(YddUserData::getUserId, userId);
        yddUserDataService.remove(userDataWrapper);
        //如果是点赞还需要减少数量
        if (type == 2) {
            YddArticle byId = this.getById(article_id);
            String lovenum = byId.getLovenum();
            Integer newLoveNum = Integer.parseInt(lovenum);
            byId.setLovenum(String.valueOf(newLoveNum - 1));
            //更新数据
            this.updateById(byId);
        }
    }
}
